﻿using Microsoft.AspNetCore.Mvc;
using System;
using ZhCun.WebUtils.DB;

namespace ZhCun.WebUtils
{
    /// <summary>
    /// 基于layui的增删查改Controller
    /// </summary>
    public abstract class BasePageCrudController : BasePageController
    {
        protected abstract string ApiControllerName { get; }

        protected virtual string AddUrl { get { return $"/api/{ApiControllerName}/add"; } }

        protected virtual string UpdateUrl { get { return $"/api/{ApiControllerName}/update"; } }

        protected virtual string DelUrl { get { return $"/api/{ApiControllerName}/del"; } }

        protected virtual string ListUrl { get { return $"/api/{ApiControllerName}/getlist"; } }

        /// <summary>
        /// 返回Edit页面之前的调用，如设置ViewModel 等重写该方法
        /// </summary>
        [NonAction]
        protected virtual IActionResult GetEditView()
        {
            return View();
        }
        /// <summary>
        /// 返回List页面之前的调用，如设置ViewModel 
        /// </summary>
        /// <returns></returns>
        [NonAction]
        protected virtual IActionResult GetListView()
        {
            return View();
        }
        /// <summary>
        /// 用户列表
        /// </summary>
        public IActionResult List()
        {
            ViewBag.DelUrl = DelUrl;
            ViewBag.ListUrl = ListUrl;
            //return View();
            return GetListView();
        }
        /// <summary>
        /// 编辑窗口,type:add or update
        /// </summary>        
        public IActionResult Edit(string type)
        {
            if ("add".Equals(type, StringComparison.CurrentCultureIgnoreCase))
            {
                ViewBag.SaveUrl = AddUrl;
            }
            else
            {
                ViewBag.SaveUrl = UpdateUrl;
            }
            return GetEditView();
        }
    }

    /// <summary>
    /// 基于layui的增删查改Controller,自创建BLL对象
    /// </summary>
    /// <typeparam name="TBLLObj">BLL类型</typeparam>
    public abstract class BasePageCrudController<TBLLObj> : BasePageCrudController
        where TBLLObj : BLLBase, new()
    {
        public BasePageCrudController()
        {
            BLLObj = new TBLLObj();
            BLLObj.SetLoginSession(this);
        }

        protected TBLLObj BLLObj { get; }
    }
}